from selenium.webdriver.common.by import By
from Page.base_page import BuyerBasePage
import logging


class CartPage(BuyerBasePage):
    def __init__(self):
        super().__init__()
        # 选中所有商品
        self.check_all_locator = (By.XPATH, '//div[@class="column"]/i[normalize-space(text()="全选")]')
        # 选中一个商品
        self.check_one_locator = (By.XPATH, '//div[@class="column "]/i')
        # 删除选中商品
        self.delete_check_locator = (By.XPATH, '//div[@class="column"]/a[1]')
        # 收藏选中商品
        self.collect_check_locator = (By.XPATH, '//div[@class="column"]/a[2]')
        # 减少商品数量
        self.decrement_goods_locator = (By.XPATH, '//a[starts-with(@id, "decrement_")]')
        # 增加商品数量
        self.increment_goods_locator = (By.XPATH, '//a[starts-with(@id, "increment_")]')
        # 修改商品数量
        self.update_goods_locator = (By.XPATH, '//input[starts-with(@name, "changeQuantity_")]')
        # 商品单价
        self.goods_price_locator = (By.XPATH, '//div/span[contains(@id, "_goods_price")]')
        # 商品小计
        self.goods_total_price_locator = (By.XPATH, '//div/span[contains(@id, "_total_price")]')
        # 删除商品
        self.delete_goods_locator = (By.XPATH, '//div/p/a[normalize-space(text()) = "删除"]')
        # 收藏商品
        self.collect_goods_locator = (By.XPATH, '//div/p/a[normalize-space(text()) = "移到我的收藏"]')
        # 商品总价
        self.total_price_locator = (By.XPATH, '//span[@class="car_sumprice"]/em')
        # 结算商品
        self.pay_goods_locator = (By.XPATH, '//div/a[@class="paytotal"]')

    # 全选商品
    def check_all_goods(self):
        self.find_el(*self.check_all_locator).click()

    # 选中一个商品
    def check_one_goods(self, n):
        els = self.find_els(*self.check_one_locator)[int(n) - 1]
        els.click()

    # 删除选中商品
    def delete_check_goods(self):
        self.find_el(*self.delete_check_locator).click()

    # 收藏选中商品
    def collect_check_goods(self):
        self.find_el(*self.collect_check_locator).click()

    # 获取商品数量
    def get_goods_count(self, n):
        els = self.find_els(*self.update_goods_locator)[int(n) - 1]
        return els.get_attribute('value')

    # 减少商品数量
    def decrement_goods(self, n):
        els = self.find_els(*self.decrement_goods_locator)[int(n) - 1]
        els.click()

    # 增加商品数量
    def increment_goods(self, n):
        els = self.find_els(*self.increment_goods_locator)[int(n) - 1]
        els.click()

    # 修改商品数量
    def update_goods(self, n, num):
        els = self.find_els(*self.update_goods_locator)[int(n) - 1]
        self.mouse_double_click(els)
        els.send_keys(num)
        # self.input_text(els, int(num))

    # 商品单价
    def get_goods_price(self, n):
        els = self.find_els(*self.goods_price_locator)[int(n) - 1]
        return els.text

    # 商品小计
    def get_goods_total_price(self, n):
        els = self.find_els(*self.goods_total_price_locator)[int(n) - 1]
        return els.text

    # 删除商品
    def delete_goods(self, n):
        els = self.find_els(*self.delete_goods_locator)[int(n) - 1]
        return els.text

    # 收藏商品
    def collect_goods(self, n):
        self.find_els(*self.collect_goods_locator)[int(n) - 1].click()

    # 商品总价
    def get_total_price(self):
        els = self.find_el(*self.total_price_locator)
        return els.text

    # 结算商品
    def pay_goods(self):
        self.find_el(*self.pay_goods_locator).click()
